package com.atguigu.protoType.improve;

/**
 * @program: design-patterns
 * @author: yangshen
 * @description: 要实现Cloneable接口，并重写clone方法
 * @create: 2022-01-23 18:16
 **/
public class Sheep implements Cloneable{
    private String name;
    private int age;
    private String color;
    /**
     * 新加属性时，重写的clone方法会自动识别，并在克隆的范围内
     */
    private String address = "蒙古羊";

    public Sheep(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                ", address='" + address + '\'' +
                '}';
    }

    @Override
    protected Object clone() {
        Sheep sheep = null;
        try {
            sheep =(Sheep) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return sheep;
    }
}
